package com.example.scaffolding.composer.worker

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.ListenableWorker
import androidx.work.testing.TestListenableWorkerBuilder
import androidx.work.workDataOf
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

/**
 * @Auther: gaoxuebin
 * @Datetime: 2021/6/8
 * @E-mail：gaoxuebin@noboauto.com
 * @desc:
 * @Goal:做大做强，再创辉煌
 */
@RunWith(AndroidJUnit4::class)
class ImageDownloadWorkerTest {

    private lateinit var context: Context

    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext()
    }

    @Test
    fun testImageDownloadWorkerWithInput() {
        val worker = TestListenableWorkerBuilder<ImageDownloadWorker>(
            context = context,
            inputData = workDataOf("IMAGE_URI" to "https://img.zcool.cn/community/01c8f15aeac135a801207fa16836ae.jpg@1280w_1l_2o_100sh.jpg")
        ).build()
        runBlocking {
            val result = worker.doWork()
            assertTrue(result is ListenableWorker.Result.Success)
        }
    }

    @Test
    @Throws(Exception::class)
    fun testImageDownloadWorkerNoInput() {
        val worker = TestListenableWorkerBuilder<ImageDownloadWorker>(
            context = context
        ).build()
        runBlocking {
            val result = worker.doWork()
            assertTrue(result is ListenableWorker.Result.Failure)
        }
    }
}